/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestorxml;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author asc
 */
public class XMLReader {

    public static Document doc;

    public static void initialize(String filename)
    {
         try {

        File fXmlFile = new File(filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static NodeList BuscaElemento(String tipo)
    {
        return doc.getElementsByTagName(tipo);
    }

    public static String getTagValue(String sTag, Node nNode) {
        Element eElement = (Element) nNode;
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
    
    public static Node getNode(String sTag, Node nNode) {
        Element eElement = (Element) nNode;
        return eElement.getElementsByTagName(sTag).item(0);
    }

    public static String getAtributeValue(String sAttr, Node nNode)
    {
        Element eElement = (Element) nNode;
        return eElement.getAttribute(sAttr);
    }

}
